$(function () {
    layui.use('layer',function () {
        var layer = layui.layer;
    });
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
    findByItem(1);
});

function findByItem(pageNO) {
    $.post("/customer/findByItem",$("#f1").serialize()+"&pageNO="+pageNO,function (info) {
        var str ="";
        for(var i=0;i<info.list.length;i++){
            var obj = info.list[i];
            str +=`<tr>
                    <td>${obj.name}</td>
                    <td>${obj.inCharge}</td>
                    <td>${obj.scope}</td>
                    <td><a href="javascript:findAllCustomer(${obj.id})" class="layui-btn layui-btn-normal">查看客户信息</a>
                    <a href="javascript:updateCustomer(${obj.id})" class="layui-btn layui-btn-warm">修改客户信息</a>
                    </td>
                    </tr>`
        }
        $("#CustomerDiv").html(str);
        if(pageNO == 1){
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'cutPageDiv', //注意，这里的 test1 是 ID，不用加 # 号
                    count: info.total, //数据总数，从服务端得到
                    limit: info.pageSize,
                    jump: function (obj, first) {
                        if (!first) {
                            findByItem(obj.curr)
                        }
                    }
                })
            })
        }
    });
}

function addCustomerAll() {
    if(($("#customerName").val() == "" && $("#customerName").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#nameError");
        return ;
    }
    if(($("#inCharge").val() == "" && $("#inCharge").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#inchargeError");
        return ;
    }
    if(($("#tel").val() == "" && $("#tel").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#telError");
        return ;
    }
    if(($("#address").val() == "" && $("#address").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#addressError");
        return ;
    }
    if(($("#email").val() == "" && $("#email").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#emailError");
        return ;
    }
    if(($("#range").val() == "" && $("#range").val().length==0)){
        layer.tips("<span>请输入送货地址</span>", "#scopeError");
        return ;
    }

    $.post("/customer/add",$("#addForm").serialize(),function (info) {
        if(info == "ok"){
            findByItem(1);
            layer.closeAll();
        }
    });

}


function addCustomer() {

    layer.open({
        title: "注册用户",//标题
        type: 1,  //弹出层的类别
        shadeClose: true,//点击遮罩关闭
        skin: 'layui-layer-rim',//加上边框
        area: ['420px', '600px'],//设置宽度高度
        anim: 2,
        content: $("#addDiv")
    });
}
function updateCustomer(id) {
    $.post("/customer/findById", "id="+id, function (info) {
        $("#supplierNameSpan").html(info.name);
        $("#principal2").val(info.inCharge);
        $("#tel2").val(info.tel);
        $("#placesSpan").html(info.address);
        $("#emailSpan1").html(info.email);
        $("#scope2").val(info.scope);
        $("#idValue").val(info.id);
    });
    layer.open({
        title: "修改信息",//标题
        type: 1,  //弹出层的类别
        shadeClose: true,//点击遮罩关闭
        skin: 'layui-layer-rim',//加上边框
        area: ['420px', '400px'],//设置宽度高度
        anim: 2,
        content: $("#updateDiv")
    })
}

function findAllCustomer(id) {
    $.post("/customer/findById", "id="+id, function (info) {
        $("#supplierNameSpan2").html(info.name);
        $("#principalSpan").html(info.inCharge);
        $("#telSpan1").html(info.tel);
        $("#placesSpan2").html(info.address);
        $("#emailSpan2").html(info.email);
        $("#scopeSpan2").html(info.scope);
    });
    layer.open({
        title: "用户详情",//标题
        type: 1,  //弹出层的类别
        shadeClose: true,//点击遮罩关闭
        skin: 'layui-layer-rim',//加上边框
        area: ['420px', '400px'],//设置宽度高度
        anim: 2,
        content: $("#findAllDiv")
    });
}

function update123() {
    $.post("/customer/update", $("#updateForm").serialize(), function (info) {
        if (info == 'ok') {
            findByItem(1);
            layer.closeAll();
        } else {
            alert("修改失败");
        }
    })
}

